import request from './request'
import { useUserStore } from '../store/modules/user'
import { CodeScanSet } from '../views/busic/base/module/coder_set/base/CodeScanning/types'
import { AxiosPromise } from 'axios'

/**
 * 查询序列号列表
 * @param {CodeAPIRequest.GetCodeList} data - 请求参数
 */
export function getCodeList(
  data: CodeAPIRequest.GetCodeList,
): AxiosPromise<CodeAPIResponse.SnCodeList> {
  return request({
    url: '/code/getList',
    method: 'post',
    data,
  })
}

/**
 * 查询序列号列表详情
 * @param id - 序列号id
 */
export const getInfo = (
  id: string,
): AxiosPromise<APIResponse<SnCodeDetailAPIResponse.SnCodeDetail>> => {
  return request({
    url: '/code/getInfo/' + id,
    method: 'get',
  })
}

// 批量激活序列号
export function activCode(data) {
  return request({
    url: '/code/activCode',
    method: 'put',

    data,
  })
}

// 批量删除序列号
export function deleteCode(data) {
  return request({
    url: '/code/deleteCode',
    method: 'DELETE',

    data,
  })
}
// 更改序列号设备信息
export function codeGoodsBindingChange(data) {
  return request({
    url: '/code/codeGoodsBindingChange',
    method: 'put',

    data,
  })
}
//获取产品下拉数据列表
export function productPageSelect(data) {
  return request({
    url: '/goods/select/productPageSelect',
    method: 'post',

    data,
  })
}
//获取产品规格下拉数据列表
export function productSpecPageSelect(data, productId) {
  return request({
    url: '/goods/select/productSpecPageSelect/' + productId,
    method: 'post',

    data,
  })
}

/**
 * 获取物料下拉数据列表
 * @param data - 请求参数
 */
export function sparePageSelect(
  data: {
    name: string
    page: number
    limit: number
    departmentId: string
    id: string
  },
  spareId?: string,
): AxiosPromise<GoodAPIResponse.SparePartOptionList> {
  const params = spareId ? { spareId } : null

  return request({
    url: '/goods/select/sparePageSelect',
    method: 'post',
    data,
    params,
  })
}

/**
 * 获取物料规格下拉数据列表
 * @param data - 请求参数
 * @param spareId - 配件id
 */
export function spareSpecPageSelect(
  data: { name: string; limit: number; page: number },
  spareId: string,
): AxiosPromise<GoodAPIResponse.SparePartSpecOptionList> {
  return request({
    url: '/goods/select/spareSpecPageSelect/' + spareId,
    method: 'post',
    data,
  })
}

/**
 * 新增序列号
 * @param data - 请求参数
 */
export function addSave(data: CodeAPIRequest.Create): AxiosPromise<APIResponse> {
  return request({
    url: '/code/save',
    method: 'post',
    data,
  })
}

// 序列号历史工单列表接口
export function getHistoryOrderList(codeId: any, data: any) {
  return request({
    url: `/code/getHistoryOrder/${codeId}`,
    method: 'post',

    data,
  })
}

// 序列号设备变更记录(序列号溯源)列表查询接口
export function codeGoodsChangeLog(data, code) {
  return request({
    url: '/codeGoodsChangeLog/getList/' + code,
    method: 'post',

    data,
  })
}
// 获取变更记录列表
export function userChangeList(data, codeId) {
  return request({
    url: '/code/userChange/getList/' + codeId,
    method: 'post',

    data,
  })
}
// 保存变更用户
export function saveChangeCodeUser(data) {
  return request({
    url: '/code/userChange/saveChangeCodeUser',
    method: 'post',

    data,
  })
}
// 根据序列号ID查询序列号操作记录列表
export function loggetList(data, codeId) {
  return request({
    url: '/code/log/getList/' + codeId,
    method: 'post',

    data,
  })
}
// 批次序列号列表
export function batchgetList(data) {
  return request({
    url: '/code/batch/getList',
    method: 'post',

    data,
  })
}
// 保存序列号关联用户登记
export function saveCodeRegister(data, codeId) {
  return request({
    url: '/code/userRegister/saveCodeRegister/' + codeId,
    method: 'post',

    data,
  })
}
// 获取用户下拉数据列表
export function selecUser(data) {
  return request({
    url: '/select/user/pageSelect',
    method: 'post',
    data,
  })
}
// 批次ID获取批次序列号详情
export function getDetail(id) {
  return request({
    url: '/code/batch/getDetail/' + id,
    method: 'get',
  })
}
// 根据批次ID获取序列号列表
export function getbatchCodeList(data, id) {
  return request({
    url: '/code/batch/getCodeList/' + id,
    method: 'post',

    data,
  })
}
// 根据序列号批次ID批量删除
export function deletebatchList(data) {
  return request({
    url: '/code/batch/',
    method: 'DELETE',

    data,
  })
}

// 获取打印模版列表
export function printTemplateList(data) {
  return request({
    url: '/code/printTemplate/getList',
    method: 'post',

    data,
  })
}
// 保存打印模版
export function savePrintTemplate(data) {
  return request({
    url: '/code/printTemplate/save',
    method: 'post',

    data,
  })
}
// 根据模版ID获取模版组件详情
export function printTemplateGetDetail(id) {
  return request({
    url: '/code/printTemplate/getDetail/' + id,
    method: 'get',
  })
}
// 批次序列号下拉列表查询
export function codeBatchPageSelect(data) {
  return request({
    url: '/code/select/codeBatchPageSelect',
    method: 'post',

    data,
  })
}
// 根据批次序列号ID获取批次序列号设备数据列表
export function codeBatchGoodsPageSelect(data, batchId) {
  return request({
    url: '/code/select/codeBatchGoodsPageSelect/' + batchId,
    method: 'post',

    data,
  })
}
// 批量删除打印模版
export function deletePrintTemplate(data) {
  return request({
    url: '/code/printTemplate/delete',
    method: 'DELETE',

    data,
  })
}
// 根据模版ID批量启用或禁用打印模版
export function activeDeletePrintTemplate(data, status) {
  return request({
    url: '/code/printTemplate/getDetail',
    method: 'put',

    data,
    params: { status },
  })
}
// 批量更改序列号设备信息
export function updateCodeGoods(data) {
  return request({
    url: '/code/batch/updateCodeGoods',
    method: 'put',

    data,
  })
}
// 序列号批次导出Excel
export function codeBatchExportExcel(excelType, batchId) {
  return request({
    url: '/dataExport/codeBatchExportExcel/' + batchId,
    method: 'get',

    params: { excelType: excelType },
    responseType: 'blob',
  })
}
//  序列号批次导出文本Txt
export function codeBatchExportTxt(batchId) {
  return request({
    url: '/dataExport/codeBatchExportTxt/' + batchId,
    method: 'get',
  })
}
//获取打印模版数据
export function getPrintTemplateData(data) {
  return request({
    url: '/code/printTemplate/getPrintTemplateData',
    method: 'post',

    data,
  })
}
// 变更用户登记地址
export function updateCodeRegisterAddress(data, codeId) {
  return request({
    url: '/code/userRegister/updateCodeRegisterAddress/' + codeId,
    method: 'put',

    data,
  })
}
// 获取未登记设备编号
export function getNotRegisterCode(
  data: CodeAPIRequest.GetNoRegisterCodeList,
): AxiosPromise<CodeAPIResponse.NoRegisterCodeList> {
  const token = { 'access-token': useUserStore().token }
  return request({
    url: '/code/getNotRegisterCode',
    method: 'post',

    data,
  })
}
// 获取未登记设备编号
export function getRegisterCode(
  data: CodeAPIRequest.GetRegisterCodeListByCustomerId,
  userId: string,
): AxiosPromise<CodeAPIResponse.RegisteredCodeList> {
  const token = { 'access-token': useUserStore().token }
  return request({
    url: '/code/getRegisterCode/' + userId,
    method: 'post',
    data,
  })
}
// 序列号导出Excel
export function codeExportExcel(data) {
  return request({
    url: '/dataExport/codeExportExcel',
    method: 'post',
    data,
    responseType: 'blob',
  })
}

// 获取序列号扫码设置信息
export function getCodeScanSet() {
  return request({
    url: '/set/getCodeScanSet',
    method: 'get',
  })
}

// 保存序列号扫码信息设置
export function saveCodeScanSet(data: Array<CodeScanSet>) {
  return request({
    url: '/set/saveCodeScanSet',
    method: 'put',
    data,
  })
}
/**根据序列号ID获取部分字段信息 */
export function getCodeFieldInfo(id: string) {
  return request({
    url: `/code/getCodeFieldInfo/${id}`,
    method: 'get',
  })
}
/**保存序列号字段信息*/
export function saveCodeFieldInfo(data) {
  return request({
    url: `/code/saveCodeFieldInfo`,
    method: 'post',
    data,
  })
}

/**
 * 根据序列号获取设备及公司登记信息
 * @param {string} code - 序列号
 */
export const getInfoByCode = (code: string): AxiosPromise<CodeAPIResponse.InfoByCode> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/code/getCodeInfoByCode/${code}`,
    method: 'get',
    headers: token,
  })
}

/**
 * 根据序列号列表获取设备列表及公司等级信息列表
 * @param {Array<string>} codeList - 序列号列表
 */
export const getInfoListByCodeList = (codeList: Array<string>) => {
  return request({
    url: '/code/getCodeInfoByCodes',
    method: 'post',
    data: codeList,
  })
}

/**
 * 根据序列号获取设备及公司登记信息(成品或物料都可查)
 * @param {String} code - 成品或物料的序列号
 */
export const getInfoByProductOrPartCode = (
  code: string,
): AxiosPromise<CodeAPIResponse.ProductOrPartSnCodeInfoByCode> => {
  return request({
    url: `/code/getCodeGOodsInfoByCode/${code}`,
    method: 'post',
  })
}

/**
 * 根据sn号查询装配清单
 * @param {string} code - 序列号
 */
export const mountingsList = (code: string) => {
  return request({
    url: '/mountings/bill/list/' + code,
    method: 'get',
  })
}

/**根据SN号获取装配清单树*/
export function getTreeBySn(sn) {
  return request({
    url: `/mountings/bill/treeBySn/${sn}`,
    method: 'get',
  })
}

/**
 * 序列号质保周期变更申请
 * @param {object} data - 请求参数
 * @param {string} data.codeId - 序列号ID
 * @param {string} data.newEndTime - 质保结束日期
 * @param {string} data.newStartTime - 质保开始日期
 */
export const updateCodeWarrantyApply = (data: {
  codeId: string
  newStartTime: string
  newEndTime: string
}): AxiosPromise<APIResponse> => {
  return request({
    url: '/code/warranty/date/log/save',
    method: 'post',
    data,
  })
}

/**
 * 获取申请更改质保周期日志列表
 * @param {string} codeId - 序列号id
 * @returns {AxiosPromise<CodeAPIResponse.CodeUpdateWarrantyDateRangeApplyLogList>}
 */
export const getUpdateWarrantyDateApplyLogList = (
  codeId: string,
): AxiosPromise<CodeAPIResponse.CodeUpdateWarrantyDateRangeApplyLogList> => {
  return request({
    url: `/code/warranty/date/log/list/${codeId}`,
    method: 'get',
  })
}

/**
 * 变更客户审核
 * @param {object} data - 请求参数
 * @param {string} data.applyMsg - 审核备注
 * @param {(1|2)} data.applyState - 审核状态：1=审核通过，2=驳回
 * @param {string} data.id - 需要审核的数据id
 */
export const updateUserCheck = (
  data: CodeAPIRequest.UpdateUserCheck,
): AxiosPromise<APIResponse> => {
  return request({
    url: '/code/userChange/apply',
    method: 'post',
    data,
  })
}

/**
 * 变更客户记录删除
 * @param {string} id - 记录id
 */
export const updateUserRecordDel = (id: string): AxiosPromise<APIResponse> => {
  return request({
    url: `/code/userChange/removeById/${id}`,
    method: 'delete',
  })
}

/**
 * 审核更改质保周期的申请
 * @param {{applyMsg: string, applyState: 1 | 2, id: string}} data - 请求参数
 * @param {string} data.applyMsg - 审核备注
 * @param {1 | 2} data.applyState - 审核状态：1=审核通过，2=驳回
 * @param {string} data.id
 * @returns {AxiosPromise<APIResponse>}
 */
export const updateWarrantyDateRangeCheck = (data: {
  applyMsg: string
  applyState: 1 | 2
  id: string
}): AxiosPromise<APIResponse> => {
  return request({
    url: '/code/warranty/date/log/apply',
    method: 'post',
    data,
  })
}

/**
 * 删除更改质保周期的申请
 * @param {string} id
 */
export const updateWarrantyDateRangeDel = (id: string): AxiosPromise<APIResponse> => {
  return request({
    url: `/code/warranty/date/log/delById/${id}`,
    method: 'delete',
  })
}

/**
 * 总部-保存更新的质保日期
 * @param {object} data - 请求参数
 * @param {string} data.id - 序列号id
 * @param {string} data.warrantyEndDate - 质保结束日期
 * @param {string} data.warrantyStartDate - 质保开始日期
 */
export const updateWarrantyRange = (data: {
  id: string
  warrantyEndDate: string
  warrantyStartDate: string
}): AxiosPromise<APIResponse> => {
  return request({
    url: '/code/saveUpdateCodeWarrantyDate',
    method: 'post',
    data,
  })
}

/**
 * 设置备用机
 * @param {object} data - 请求参数
 * @param {string} data.id - 编码id
 * @param {0|1} data.standby - 0-否，1-是
 */
export const setBackupMachine = (data: {
  id: string
  standby: 0 | 1
}): AxiosPromise<APIResponse> => {
  return request({
    url: '/code/updateCodeExtend',
    method: 'post',
    data,
  })
}

/**
 * 根据配件ID获取序列号下拉选项
 * @param {object} data - 请求参数
 * @param {string} data.spareId - 物料id
 * @param {string} data.code - 序列号精搜专用
 */
export const getSnCodeOptionListBySparePartId = (data: {
  spareId: string
  code: string
}): AxiosPromise<NewAPIResponse<Array<OrderSparePartAPIResponse.SnCodeOptionBySparePartId>>> => {
  return request({
    url: '/code/spare',
    method: 'post',
    data,
  })
}

/**
 * 更改工图
 * @param data - 请求参数
 * @param data.id - 序列号id
 * @param data.cadUrlList - CAD文件的URL
 * @param data.bfFileUrlList - 备份文件的URL
 * @param data.keywordUrlList - 加密证书文件的URL
 */
export const codeUpdateWorkDrawing = (
  data: CodeAPIRequest.UpdateWorkDrawingParam,
): AxiosPromise<APIResponse> => {
  return request({
    url: '/code/codeCadDrawingUpdate',
    method: 'put',
    data,
  })
}
